package icu.cocoon.system.config;

import java.util.Collections;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author Cocoon
 */
@Configuration
public class OpenApiConfig {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
//        .apis(RequestHandlerSelectors.basePackage("icu.cocoon.system"))
        .paths(PathSelectors.any())
        .build()
        .securityContexts(Collections.singletonList(securityContexts()))
        .securitySchemes(Collections.singletonList(securitySchemes()));
  }

  private SecurityScheme securitySchemes() {
    return new ApiKey("Authorization", "Authorization", "header");
  }

  private SecurityContext securityContexts() {
    return SecurityContext.builder()
        .securityReferences(defaultAuth())
//        .operationSelector(PathSelectors.none())
        .build();
  }

  private List<SecurityReference> defaultAuth() {
    AuthorizationScope authorizationScope = new AuthorizationScope("xxx", "描述信息");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;
    return Collections.singletonList(new SecurityReference("Authorization", authorizationScopes));
  }





  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("Cocoon")
        .description("Cocoon")
        .contact(new Contact("Cocoon", "http://cocoon.icu", "6631097@qq.com"))
        .build();
  }

}
